package com.seis_645.appTesting;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DealerDataSource {
	// database fields
	private SQLiteDatabase database;
	private MySQLiteHelper db_helper;
	private String[] all_columns = {MySQLiteHelper.DLR_ID, 
			MySQLiteHelper.DLR_ADDR_ID, MySQLiteHelper.DLR_NAME,
			MySQLiteHelper.DLR_CLEANLINESS, MySQLiteHelper.DLR_FRIENDLINESS,
			MySQLiteHelper.DLR_SUPPLY};
	
	public DealerDataSource(Context context) {
		this.db_helper = new MySQLiteHelper(context);
	}
	
	public void open() throws SQLException {
		database = this.db_helper.getWritableDatabase();
	}
	
	public void close() {
		db_helper.close();
	}
	
	public Dealer create_dealer(String name, String street, String city, String state, String zipcode, int cleanliness, int friendliness, int supply) {
		ContentValues addr_values = new ContentValues();
		ContentValues dlr_values = new ContentValues();
		addr_values.put(MySQLiteHelper.ADDR_STREET,street);
		addr_values.put(MySQLiteHelper.ADDR_CITY,city);
		addr_values.put(MySQLiteHelper.ADDR_STATE,state);
		addr_values.put(MySQLiteHelper.ADDR_ZIP,zipcode);
		
		long address_id = this.database.insert(MySQLiteHelper.TBL_ADDRESS, null, addr_values);
		
		dlr_values.put(MySQLiteHelper.DLR_NAME,name);
		dlr_values.put(MySQLiteHelper.DLR_ADDR_ID,address_id);
		dlr_values.put(MySQLiteHelper.DLR_CLEANLINESS, cleanliness);
		dlr_values.put(MySQLiteHelper.DLR_FRIENDLINESS, friendliness);
		dlr_values.put(MySQLiteHelper.DLR_SUPPLY, supply);
		
		long insert_id = this.database.insert(MySQLiteHelper.TBL_DEALER, null, dlr_values);
		
		Cursor cursor = this.database.query(MySQLiteHelper.TBL_DEALER,this.all_columns,MySQLiteHelper.DLR_ID + " = " + insert_id, null,null,null,null);
		cursor.moveToFirst();
		Dealer new_dealer = cursor_to_dealer(cursor);
		cursor.close();
		
		return new_dealer;
	}
	
	public void update_dealer(long id, long addr_id , String name, String street, String city, String state, String zipcode, int cleanliness, int friendliness, int supply) {
		ContentValues addr_values = new ContentValues();
		ContentValues dlr_values = new ContentValues();
		addr_values.put(MySQLiteHelper.ADDR_STREET,street);
		addr_values.put(MySQLiteHelper.ADDR_CITY,city);
		addr_values.put(MySQLiteHelper.ADDR_STATE,state);
		addr_values.put(MySQLiteHelper.ADDR_ZIP,zipcode);
		
		this.database.update(MySQLiteHelper.TBL_ADDRESS,addr_values, MySQLiteHelper.ADDR_ID + " = " + addr_id,null);
		
		dlr_values.put(MySQLiteHelper.DLR_NAME,name);
		dlr_values.put(MySQLiteHelper.DLR_ADDR_ID,addr_id);
		dlr_values.put(MySQLiteHelper.DLR_CLEANLINESS, cleanliness);
		dlr_values.put(MySQLiteHelper.DLR_FRIENDLINESS, friendliness);
		dlr_values.put(MySQLiteHelper.DLR_SUPPLY, supply);
		
		this.database.update(MySQLiteHelper.TBL_DEALER, dlr_values, MySQLiteHelper.DLR_ID + " = " + id,null);
		
		
		
	}
	
	public void delete(Dealer dealer) {
		long id = dealer.get_id();
		System.out.println("Comment deleted with id: " + id);
		database.delete(MySQLiteHelper.TBL_DEALER, MySQLiteHelper.DLR_ID
				+ " = " + id, null);
	}
	
	public List<Dealer> get_all_dealers() {
		List<Dealer> dlrs = new ArrayList<Dealer>();
		
		Cursor cursor = this.database.query(MySQLiteHelper.TBL_DEALER,this.all_columns,null,null,null,null,null);
		cursor.moveToFirst();
		while(!cursor.isAfterLast()) {
			Dealer dlr = cursor_to_dealer(cursor);
			dlrs.add(dlr);
			cursor.moveToNext();
		}
		cursor.close();
		return dlrs;
	}
	
	public Dealer get_dealer(long id) {
		Cursor cursor = this.database.query(MySQLiteHelper.TBL_DEALER,this.all_columns,MySQLiteHelper.DLR_ID + " = " + id,null,null,null,null);
		cursor.moveToFirst();
		Dealer dealer = cursor_to_dealer(cursor);
		
		return dealer;
	}
	
	private Dealer cursor_to_dealer(Cursor cursor) {
		Dealer dealer = new Dealer(cursor.getLong(1),cursor.getString(2), cursor.getInt(3), cursor.getInt(4), cursor.getInt(5));
		dealer.set_id(cursor.getLong(0));
		return dealer;
	}
	
}
